package com.zhuchaooasystem.ui.common;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.LocalAlbumHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.presenter.common.LocalAlbumContract;
import com.zhuchaooasystem.presenter.common.LocalAlbumPresenter;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LocalAlbumActivity extends BaseActivity<LocalAlbumPresenter> implements LocalAlbumContract.View {

    private final int IMAGE_MAX_DEFAULT = 9;

    @BindView(R.id.actionbar_txt_title)
    TextView txt_title;
    @BindView(R.id.actionbar_iv_back)
    ImageView iv_back;
    //----
    @BindView(R.id.act_local_album_recycle)
    EasyRecyclerView recycle;
    @BindView(R.id.act_local_album_txt_confirm)
    TextView txt_confirm;

    //----每行放置的图片个数
    private final int numColomn = 3;
    private int image_max;  //可以选择的图片最大数
    private int image_cur;  //当前已选择的图片数
    //----选中的图片路径列表
    private List<Integer> checkPositionlist; //选中图片位置的列表
    private ArrayList<String> imageChoosedList; //选中的图片列表
    private ArrayList<String> imageList;
    private RecyclerArrayAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_local_album;
    }

    @Override
    protected LocalAlbumPresenter getPresenter() {
        return new LocalAlbumPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        if (null != getIntent() && null != getIntent().getExtras()) {
            image_max = getIntent().getExtras().getInt("image_max", IMAGE_MAX_DEFAULT);
            image_cur = getIntent().getExtras().getInt("image_cur", 0);
        }
        imageChoosedList = new ArrayList<>();
        checkPositionlist = new ArrayList<>();
    }

    @Override
    protected void initView() {
        setActionBar();
        //----
        initRecycle();
    }

    private void initRecycle() {
        //----
        recycle.setLayoutManager(new GridLayoutManager(mContext, numColomn));
        recycle.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<String>(mContext) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new LocalAlbumHolder(parent, LocalAlbumActivity.this);
            }
        });

    }

    private void setActionBar() {
        txt_title.setText(getString(R.string.actionbar_title_latest_photo));
        iv_back.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initDataAndLoadData() {
        //权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //请求权限
            requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, Key.EXTERNAL_STORAGE);
        } else {
            //----
            mPresenter.getData();
        }
    }

    @OnClick({R.id.actionbar_iv_back, R.id.act_local_album_txt_review, R.id.act_local_album_txt_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                finish();
                break;
            //预览按钮
            case R.id.act_local_album_txt_review:
                //----
                if (null != imageChoosedList && imageChoosedList.size() > 0) {
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("images", imageChoosedList);
                    bundle.putBoolean("isCache", false);
                    AppUtils.jump(mContext, ImagePagerActivity.class, bundle);

                } else {
                    ToastUtil.showShort(mContext, "请选择要预览的图片");
                }
                break;
            //确认按钮
            case R.id.act_local_album_txt_confirm:
                RxBus.get().post("localAlbum", new BaseEvent<>(imageChoosedList));
                finish();
                break;
        }
    }

    public void setTxtConfirm(int size) {
        if (size > 0) {
            txt_confirm.setText("确定(" + imageChoosedList.size() + ")");
            txt_confirm.setBackgroundResource(R.drawable.shape_act_local_album_confirm_checked);
            txt_confirm.setTextColor(Color.WHITE);
        } else {
            txt_confirm.setText("确定");
            txt_confirm.setBackgroundResource(R.drawable.shape_act_local_album_confirm_uncheck);
            txt_confirm.setTextColor(getResources().getColor(R.color.color_1bc3ad));
        }
    }

    @Override
    public void getEmpty() {
        ToastUtil.showShort(mContext, "暂未获取到本地图片");
    }

    @Override
    public void refreshData(ArrayList<String> imageList) {
        this.imageList = imageList;
        adapter.clear();
        adapter.addAll(imageList);
    }

    @Override
    public void getError() {
        ToastUtil.showShort(mContext, "获取本地图片失败");
    }

    public ArrayList<String> getImageChoosedList() {
        return imageChoosedList;
    }

    public void setImageChoosedList(ArrayList<String> imageChoosedList) {
        this.imageChoosedList = imageChoosedList;
    }

    public List<Integer> getCheckPositionlist() {
        return checkPositionlist;
    }

    public void setCheckPositionlist(List<Integer> checkPositionlist) {
        this.checkPositionlist = checkPositionlist;
    }

    public int getNumColomn() {
        return numColomn;
    }

    public int getImage_cur() {
        return image_cur;
    }

    public void setImage_cur(int image_cur) {
        this.image_cur = image_cur;
    }

    public int getImage_max() {
        return image_max;
    }

    public void setImage_max(int image_max) {
        this.image_max = image_max;
    }

    public ArrayList<String> getImageList() {
        return imageList;
    }

    public void setImageList(ArrayList<String> imageList) {
        this.imageList = imageList;
    }

    /**
     * 权限成功回调函数
     *
     * @param requestCode
     */
    @Override
    public void permissionSuccess(int requestCode) {
        super.permissionSuccess(requestCode);
        switch (requestCode) {
            case Key.EXTERNAL_STORAGE:
                mPresenter.getData();
                break;
        }
    }

    /**
     * 权限失败回调函数
     *
     * @param requestCode
     */
    @Override
    public void permissionFail(int requestCode) {
        super.permissionFail(requestCode);
        Toast.makeText(mContext, "获取权限失败", Toast.LENGTH_SHORT).show();
    }

}
